package com.shizhanzhe.zxbapp.my;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.bean.UserLM;
import com.shizhanzhe.zxbapp.customtask.ApprovalActivity;
import com.shizhanzhe.zxbapp.pay.PayVIPActivity;
import com.shizhanzhe.zxbapp.ui.BindingPhoneActivity;
import com.shizhanzhe.zxbapp.ui.LoginActivity;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.DataCleanManager;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.utils.SharedPreferencesUtils;
import com.shizhanzhe.zxbapp.vip.MyPDDActivity;
import com.shizhanzhe.zxbapp.widget.BindDialog;
import com.shizhanzhe.zxbapp.widget.CircleImageView;
import com.shizhanzhe.zxbapp.widget.CodePopupWindow;
import com.shizhanzhe.zxbapp.widget.ErrorCallback;
import com.shizhanzhe.zxbapp.widget.ExitDialog;
import com.shizhanzhe.zxbapp.widget.JDAdverView;
import com.shizhanzhe.zxbapp.widget.VipDIalog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MyFragment extends Fragment {


    TextView userid;
    TextView dangqianyuer;
    Unbinder unbinder;
    @BindView(R.id.nickname)
    TextView nickname;
    @BindView(R.id.headimg)
    CircleImageView headimg;
    @BindView(R.id.tixianarea)
    RelativeLayout tixianarea;
    @BindView(R.id.tv_phone_num)
    TextView tvPhoneNum;
    private View mypublish;
    private View mypdd;
    LoadService loadService;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        //第一步：获取布局View
        View rootView=View.inflate(getActivity(),R.layout.fragment_my,  null);
        unbinder = ButterKnife.bind(this, rootView);

        //第二步：注册布局View
        loadService = LoadSir.getDefault().register(rootView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                // 重新加载逻辑
                money(UserBean.uid);
            }
        });
        //第三步：返回LoadSir生成的LoadLayout
        return loadService.getLoadLayout();
    }



    protected ImmersionBar mImmersionBar;

    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init();
        mImmersionBar
                .statusBarDarkFont(true, 0.2f)
                .init();
    }

    @SuppressLint("SetTextI18n")
    public void initData() {
        setMsg();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initImmersionBar();
        userid = (TextView) view.findViewById(R.id.userid);
        dangqianyuer = (TextView) view.findViewById(R.id.dangqianyuer);
        //个人中心
        view.findViewById(R.id.ll_myinfo_editinfo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.uid != 0) {
                    startActivity(new Intent(getActivity(), MyInfoActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });
        view.findViewById(R.id.headimg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.uid != 0) {
                    startActivity(new Intent(getActivity(), MyInfoActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });
        //我的消息
        view.findViewById(R.id.rl_myinfo_msg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), MyMessageActivity.class));
            }
        });
        //我发布的任务
        mypublish = view.findViewById(R.id.mypublish);
        mypublish.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), ApprovalActivity.class));
            }
        });
        //我的返佣
        mypdd = view.findViewById(R.id.mypdd);
        mypdd.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), MyPDDActivity.class));
            }
        });
        //绑定手机
        view.findViewById(R.id.bangding_phone).setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), BindingPhoneActivity.class));
            }
        });

        //商务合作
        view.findViewById(R.id.shangwu).setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), CooperationActivity.class));
            }
        });
        //常见问题
        view.findViewById(R.id.changjian).setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), CommonProblemActivity.class));
            }
        });
        //任务记录
        view.findViewById(R.id.renwujilurarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.uid != 0) {
                    startActivity(new Intent(getActivity(), TaskRecordActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });
        //提现
        view.findViewById(R.id.tixianarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.uid != 0) {
                    if ("".equals(UserBean.mobile)) {
                        final BindDialog bindDIalog = new BindDialog(getContext());
                        bindDIalog.setBindListener(new BindDialog.BindListener() {
                            @Override
                            public void chooseTrue() {
                                startActivity(new Intent(getActivity(), BindingPhoneActivity.class));
                                bindDIalog.dismiss();
                            }

                            @Override
                            public void chooseFalse() {
                                bindDIalog.dismiss();
                            }
                        });
                        bindDIalog.show();
                        bindDIalog.setContent();

                    }else {
                        startActivity(new Intent(getActivity(), TiXianActivity.class));
                    }

                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });
        //提现记录
        view.findViewById(R.id.wodetixianarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.uid != 0) {
                    startActivity(new Intent(getActivity(), TiRecordActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });

        //邀请好友
        view.findViewById(R.id.yaoqing).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.uid != 0) {
                    startActivity(new Intent(getActivity(), ShareCodeActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });
        //二维码
        view.findViewById(R.id.rl_ewm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.uid != 0) {

                    CodePopupWindow popupWindow = new CodePopupWindow(getActivity());
                    popupWindow.showDown(nickname);
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });
        view.findViewById(R.id.iv_myinfo_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), SettingActivity.class));
            }
        });
        initData();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (UserBean.level>0){
            mypublish.setVisibility(View.VISIBLE);
            mypdd.setVisibility(View.VISIBLE);
        }
        setMsg();
    }

    @SuppressLint("SetTextI18n")
    private void setMsg() {
        if (UserBean.uid != 0) {
            userid.setText(UserBean.uid + "");
            if (!"".equals(UserBean.nickname)) {
                nickname.setText(UserBean.nickname);
            } else {
                nickname.setText("新用户");
            }
            if (!"".equals(UserBean.mobile)){
                tvPhoneNum.setText(UserBean.mobile.substring(0,3) + "****" + UserBean.mobile.substring(7,UserBean.mobile.length()));
            }
            Glide.with(getContext()).load(Path.BASE_URL + "/" + UserBean.headimg)
                    .apply(new RequestOptions().error(R.mipmap.head)).into(headimg);
            money(UserBean.uid);
        } else {
            userid.setText("00000");
            nickname.setText("未登录");
            dangqianyuer.setText("0.00");
        }
    }

    private void money(int uid) {
        OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.money + "?uid=" + uid, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {

                if (!"1".equals(json) && !"2".equals(json)) {
                    try{


                    Gson gson = new Gson();
                    UserLM userLM = gson.fromJson(json, UserLM.class);
                    UserBean.balance = userLM.getInfo().getBalance();
                    UserBean.nickname=userLM.getInfo().getNickname();
                    UserBean.gao=userLM.getInfo().getGaotype();
                    nickname.setText(UserBean.nickname);
                    if (UserBean.level>0){
                        mypublish.setVisibility(View.VISIBLE);
                        mypdd.setVisibility(View.VISIBLE);
                    }
                    SharedPreferencesUtils.setParam(getContext(), "nickname", UserBean.nickname);
                    SharedPreferencesUtils.setParam(getContext(), "balance", UserBean.balance);
                    dangqianyuer.setText(UserBean.balance);
                        loadService.showSuccess();
                    }catch (Exception e){
                        loadService.showCallback(ErrorCallback.class);
                    }
                }else {
                    loadService.showCallback(ErrorCallback.class);
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        if (mImmersionBar != null)
            mImmersionBar.destroy();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (UserBean.uid!=0) {
            money(UserBean.uid);
        }
        if (!hidden && mImmersionBar != null)
            ImmersionBar.with(this)
                    .statusBarDarkFont(true, 0.2f)
                    .init();
    }
}
